package com.lz.sports.vm

import android.os.SystemClock
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lz.comm.SP
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.library.base.SingleLiveData
import com.lz.library.net.NetworkManager
import com.lz.library.net.NetworkType
import com.lz.sports.bean.MainModule
import com.lz.sports.core.*
import com.lz.sports.service.ServiceStatusManager

/**
 *  author: you : 2022/11/2
 */
class MainViewModel : BaseViewModel<BaseModel>() {

    companion object {

        const val MAX_DELAY = 1000
        //连续5次进入重置功能
        const val MAX_COUNT = 5
    }
    //是否为超级管理员权限
    val isSuperManager = MutableLiveData<Boolean>()

    //连续点击五次
    private var lastTime = 0L
    private var numTime = 0
    val showReset = SingleLiveData(false)

    val deviceNum = MutableLiveData<String>()

    //主界面模块
    val mainModels = MutableLiveData<List<MainModule>>()

    //网络状态和服务器网络状态
    val isNetwork = MutableLiveData(NetworkManager.isNetworkConnected())
    val isServiceNetwork = MutableLiveData(ServiceStatusManager.isNetwork)

    override fun onCreate(owner: LifecycleOwner) {
        val isSuperManager = SP.getBoolean(K_IS_SUPER_MANAGER, v_isSuperManager)
        this.isSuperManager.value = isSuperManager
        val list = ArrayList<MainModule>()
        if (isSuperManager || SP.getBoolean(K_MODULE_CHECK_IN, v_moduleCheckIn)) {
            list.add(MainModule.CheckInModule)
        }
        if (isSuperManager || SP.getBoolean(K_MODULE_SIGN_IN, v_moduleSignIn)) {
            list.add(MainModule.SignInModule)
        }
        if (isSuperManager || SP.getBoolean(K_MODULE_DATA_SELECT, v_moduleDataSelect)) {
            list.add(MainModule.DataSelectModule)
        }
        if (isSuperManager || SP.getBoolean(K_MODULE_RESULT_CONFIRM, v_moduleResultConfirm)) {
            list.add(MainModule.ScoreSureModule)
        }
        mainModels.value = list

        NetworkManager.observe(owner, object : NetworkManager.StateObserver{
            override fun onStateChanged(type: NetworkType) {
                isNetwork.value = type != NetworkType.NONE
            }
        })

        ServiceStatusManager.observe(owner, object : ServiceStatusManager.StatusObserver {
            override fun onStatusChanged(status: Boolean) {
                isServiceNetwork.value = status
            }
        })
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        deviceNum.value = SP.getString(K_DEVICE_NAME, v_deviceName)
    }

    val test = SingleLiveData(false)

    fun titleClick() {
        if (isSuperManager.value == false) {
            val currTime = SystemClock.elapsedRealtime()
            if ((currTime - lastTime) > MAX_DELAY) {
                numTime = 1
            } else {
                numTime++
                if (numTime >= MAX_COUNT) {
                    showReset.setValue(true)
                    numTime = 0
                }
            }
            lastTime = currTime
        } else {
            val currTime = SystemClock.elapsedRealtime()
            if ((currTime - lastTime) > MAX_DELAY) {
                numTime = 1
            } else {
                numTime++
                if (numTime >= MAX_COUNT) {
                    test.setValue(true)
                    numTime = 0
                }
            }
            lastTime = currTime
        }
    }

    fun reset() {
        SP.setBoolean(K_IS_SUPER_MANAGER, v_isSuperManager)
        SP.setBoolean(K_MODULE_CHECK_IN, v_moduleCheckIn)
        SP.setBoolean(K_MODULE_SIGN_IN, v_moduleSignIn)
        SP.setBoolean(K_MODULE_DATA_SELECT, v_moduleDataSelect)
        SP.setBoolean(K_MODULE_RESULT_CONFIRM, v_moduleResultConfirm)
    }
}